import {
    validatenull,
} from '@/util/validate';
import website from '@/const/website';
import {
    setStatusBarStyle,
} from '@/cordova/';

import router from '@/router/router';

import { mapGetters } from 'vuex';

export default (params) => {
  params = params || {};
  const {
        status,
    } = params;
  return {
      created() {
          setStatusBarStyle(status || 0);
        },
      computed: {
          ...mapGetters(['token', 'userInfo', 'website']),
        },
      methods: {
          goAuth() {
              if (this.userInfo.userType === this.website.rolesType.YZ || validatenull(this.userInfo.userType)) {
                  if (validatenull(this.userInfo.houseId)) {
                      this.$router.push({ path: '/auth' });
                    } else {
                      this.$router.replace({ path: '/index/main' });
                    }
                } else {
                  this.$router.push({ path: '/roles' });
                }
            },
          goLink(item) {
              if (!validatenull(item.event)) {
                  this[item.event](item);
                } else {
                  this.goPage(item.href, item.title, item.query, item.replace);
                }
            },
          setPage(res) {
              const data = res.data;
              const list = data.records;
              this.page.total = data.total;
              if (validatenull(list) && this.page.page === 1) {
                  this.finished = true;
                  this.loading = false;
                  this.tip = true;
                } else if (this.page.page === 1) {
                  this.tip = false;
                  this.list = list;
                } else if (this.page.page >= this.page.total / this.page.limit) {
                  this.finished = true;
                } else {
                  this.list = this.list.concat(list);
                }
              this.loading = false;
              this.page.page = this.page.page + 1;
            },
          goBack(page) {
              page = page || -1;
              router.isBack = true;
              router.go(page);
            },
          goPage(href, title, query, replace) {
              if (validatenull(href)) {
                  this.$message({
                      message: website.errPage,
                      duration: 1500,
                    });
                  return;
                }
              if (href.indexOf('tel') != -1) {
                  location.href = href;
                } else if (href.indexOf('http') != -1 || href.indexOf('https') != -1) {
                  this.$router.push({
                      path: '/iframe/urlPath',
                      query: Object.assign({
                          src: href,
                          title,
                        }, query),
                    });
                } else if (replace) {
                  this.$router.replace({
                      path: href,
                      query,
                    });
                } else {
                  this.$router.push({
                      path: href,
                      query,
                    });
                }
            },
        },
    };
};
